/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.h5.controller.order;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.OrderInfoPo;
import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.IOrderInfoConnector;
import com.itttq.gshop.generic.connector.IOrderWareConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.generic.util.Constants.OrderStatus;
@Auth
@RestController
@RequestMapping("order")
public class OrderController extends GenericController {
	private static final long serialVersionUID = 456162093189751748L;
	@Autowired
	private IOrderInfoConnector orderInfoConnector;
	@Autowired
	private IOrderWareConnector orderWareConnector;
	
	/**
	 * 根据条件查询OrderInfoPo对象，并分页
	 * 
	 * @param word		查询的条件
	 * @param request	
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("orders.html")
	public ModelAndView getOrders(HttpServletRequest request) {
		return new ModelAndView("/order/orders");
	}
	
	/**
	 * 获取订单信息以及其他通用处理调用
	 * 
	 * @param order
	 * @Author 迷蝶
	 * @return void
	 */
	@RequestMapping("orderContent.html")
	public ModelAndView getOrders(OrderInfoPo order) {
		ModelAndView mv = new ModelAndView("/order/orders-content");
		mv.addObject("orders", orderInfoConnector.getOrderWares(order));
//		mv.addObject("sources", Source.values());
//		mv.addObject("status", OrderStatus.values());
		return mv;
	}
	
	/**
	 * 根据条件查询已付款待发货订单，并分页
	 * 
	 * @param request
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("paid.html")
	public ModelAndView paidOrders(HttpServletRequest request) {
		return getOrders(getOrderParam(OrderStatus.PAID, request));
	}
	/**
	 * 根据条件查询申请退款订单，并分页
	 * 
	 * @param request
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("refundApply.html")
	public ModelAndView refundApplyOrders(HttpServletRequest request) {
		return getOrders(getOrderParam(OrderStatus.REFUND_APPLY, request));
	}
	/**
	 * 根据条件查询申请退货订单，并分页
	 * 
	 * @param request
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("refundGoods.html")
	public ModelAndView refundGoodsOrders(HttpServletRequest request) {
		return getOrders(getOrderParam(OrderStatus.REFUND_GOODS, request));
	}
	
	private OrderInfoPo getOrderParam(OrderStatus os, HttpServletRequest request) {
		OrderInfoPo order = new OrderInfoPo();
		order.setStatus(os.getVal());
		this.getPpt(request, order);
		return order;
	}

	/**
	 * 根据订单ID更新订单状态，并记录到订单操作记录。<br>
	 * 订单变化限制如下：前面的状态必须由后面的状态变化而来
	 * 待支付1：无<br>
	 * 待发货2：<--待支付1；<br>
	 * 待收货3：<--待发货2；<br>
	 * 已完成4：<--待收货3；<br>
	 * 已取消5：<--待支付1；<br>
	 * 申请退款7：<--待发货2、退货中12；<br>
	 * 退款中8：<--申请退款7；<br>
	 * 已退款9：<--退款中8；<br>
	 * 退款失败10：<--申请退款7；<br>
	 * 申请退货11：<--待收货3；<br>
	 * 退货中12：<--申请退货11；<br>
	 * 退货失败15：<--申请退货11；<br>
	 * 删除16：<--已完成4、已取消5、已退款9、退款失败10、退货失败15<br>
	 * 取消退款：<--申请退款7<br>
	 * 取消退货：<--申请退货11
	 * 
	 * @param id	订单ID
	 * @param status	订单更新后的状态
	 * @param op	操作类型
	 * @param remark	备注
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("status/{id}.html")
	public ResultTo updateOrderStatus(HttpServletRequest request, @PathVariable("id")int id, 
			String status, String op, String remark) {
		// 由于有登录校验，此处正常不会为空，所以直接转换
		SysUserPo user = (SysUserPo) this.getSession(request, Constants.SESSION_USER);
		return orderInfoConnector.updateOrderStatus(id, status, op, remark, user.getId(), user.getUsername());
	}
}